/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.gt.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.gt.entity.GtCountry;
import com.thinkgem.jeesite.modules.gt.service.GtCountryService;

/**
 * 下乡重点村信息Controller
 * @author zad
 * @version 2018-03-26
 */
@Controller
@RequestMapping(value = "${frontPath}/gt/gtCountry")
public class GtCountryController extends BaseController {

	@Autowired
	private GtCountryService gtCountryService;
	
	@ModelAttribute
	public GtCountry get(@RequestParam(required=false) String id) {
		GtCountry entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = gtCountryService.get(id);
		}
		if (entity == null){
			entity = new GtCountry();
		}
		return entity;
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(GtCountry gtCountry, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<GtCountry> page = gtCountryService.findPage(new Page<GtCountry>(request, response), gtCountry); 
		model.addAttribute("page", page);
		return "modules/gt/basic/gtCountryList";
	}

	@RequestMapping(value = "form")
	public String form(GtCountry gtCountry, Model model) {
		model.addAttribute("gtCountry", gtCountry);
		return "modules/gt/basic/gtCountryForm";
	}

	@RequestMapping(value = "save")
	public String save(GtCountry gtCountry, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, gtCountry)){
			return form(gtCountry, model);
		}
		gtCountryService.save(gtCountry);
		addMessage(redirectAttributes, "保存下乡重点村信息成功");
		return "redirect:"+super.frontPath+"/gt/gtCountry/?repage";
	}
	
	@RequestMapping(value = "delete")
	public String delete(GtCountry gtCountry, RedirectAttributes redirectAttributes) {
		gtCountryService.delete(gtCountry);
		addMessage(redirectAttributes, "删除下乡重点村信息成功");
		return "redirect:"+super.frontPath+"/gt/gtCountry/?repage";
	}

}